unit uniDlgRelClientes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, DBCtrls, StdCtrls, DB, ADODB;

type
  TfrmDlgRelClientes = class(TForm)
    GroupBox2: TGroupBox;
    Label2: TLabel;
    dblRepresentante: TDBLookupComboBox;
    Panel2: TPanel;
    btnFechar: TSpeedButton;
    btnImprimir: TSpeedButton;
    Label1: TLabel;
    dblCliente: TDBLookupComboBox;
    QR_Cliente: TADOQuery;
    DS_Cliente: TDataSource;
    QR_Representante: TADOQuery;
    DS_Representante: TDataSource;
    procedure btnImprimirClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnFecharClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDlgRelClientes: TfrmDlgRelClientes;

implementation

uses uniRelClientes;

{$R *.dfm}

procedure TfrmDlgRelClientes.btnImprimirClick(Sender: TObject);
var sSQL,
    sWhere:string;

begin
  sWhere := '';
  if dblCliente.Text <> '' then
    sWhere := sWhere + ' AND C.CD_CLIENTE = '+IntToStr(dblCliente.KeyValue);

  if dblRepresentante.Text <> '' then
    sWhere := sWhere + ' AND C.CD_REPRESENTANTE = '+IntToStr(dblRepresentante.KeyValue);

  sSQL := ' SELECT C.*, R.NOME, M.DESCRICAO FROM TB_CLIENTE C, TB_MARCA M, TB_REPRESENTANTE R'+
          ' WHERE C.CD_CLIENTE = M.CD_CLIENTE '+
          ' AND   C.CD_REPRESENTANTE =R.CD_REPRESENTANTE '+
          sWhere +
          ' ORDER BY RAZAO ';
   try
    Application.CreateForm(TfrmRelClientes, frmRelClientes);
    frmRelClientes.QR_Rel.close;
    frmRelClientes.QR_Rel.SQL.Text := sSQL;
    frmRelClientes.QR_Rel.Open;
    frmRelClientes.qrpRel.Preview;
   finally
     frmRelClientes.Free;
   end;  
end;

procedure TfrmDlgRelClientes.FormShow(Sender: TObject);
begin
 //Abre a qr_cliente
 QR_Cliente.Close;
 QR_Cliente.Open;

 //Abre QR_Representante
 QR_Representante.Close;
 QR_Representante.Open;
end;

procedure TfrmDlgRelClientes.btnFecharClick(Sender: TObject);
begin
close;
end;

end.
